from django.shortcuts import render

# Create your views here.
from rest_framework.generics import CreateAPIView
from .models import Order
from .serializers import OrderModelSerializer
from rest_framework.permissions import IsAuthenticated
class OrderAPIView(CreateAPIView):
    """用户下单"""
    queryset = Order.objects.all()
    serializer_class = OrderModelSerializer
    permission_classes = [IsAuthenticated]

from rest_framework.response import Response
from rest_framework.generics import ListAPIView
from .serializers import OrderListModelSerializer

class OrderListAPIView(ListAPIView):
    queryset = Order.objects.all().order_by("-id")
    permission_classes = [IsAuthenticated]
    serializer_class = OrderListModelSerializer
    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        queryset.filter(user_id=request.user.id)
        queryset = self.filter_queryset(queryset)

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)